package com.easy.cart.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.easy.bean.Carts;
import com.easy.cart.mapper.ICartMapper;
import com.easy.cart.service.ICartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
@Service
public class CartServlceImpl implements ICartService {
    @Autowired
    ICartMapper cartM;

    @Override
    public int addCarts(Carts carts) {
        int result=0;
        QueryWrapper<Carts> qw=new QueryWrapper<>();
        qw.eq("userid",carts.getUserid());
        qw.eq("productid",carts.getProductid());
        long count=cartM.selectCount(qw);
        if(count>0){
            //购物车中已经有对应的商品数据
            result=  cartM.addCartCount(carts);
        }else{
            result= cartM.insert(carts);
        }
        return result;
    }

    @Override
    public int deleteCarts(Integer id) {
       return cartM.deleteById(id);
    }

    @Override
    public List<Carts> getCartsList(Integer userid) {
        return cartM.getCartList(userid);
    }

    @Override
    public int editCartsCount(Integer id, Integer num) {
        UpdateWrapper<Carts> uw=new UpdateWrapper<>();
        uw.eq("id",id);
        uw.set("quantity",num);
        return cartM.update(uw);
    }

    @Override
    public int editCartsSelect(int id,int selected) {
        UpdateWrapper<Carts> uw=new UpdateWrapper<>();
        uw.eq("id",id);
        uw.set("selected",selected);
        return cartM.update(uw);
    }

    public int deleteCart(int userid,int productid){
       return  cartM.deleteCart(userid, productid);
    }
}
